
Page({

    /**
     * 页面的初始数据
     */
    data: {
          hasResult:-1,
          bar_state:0,
          winWidth:0,
          winHeight:0,
          hobby: '篮球', // 指定的爱好
          matchedUsers: [], // 匹配到的用户列表
    },
    onButtonClick: function () {
        // 先执行startAnimation逻辑
        this.startAnimation();
        
        // 再执行onMatchClick逻辑
        this.onMatchClick();
      },
    
      // 点击按钮触发匹配
      onMatchClick: function () {
        const that = this;
        wx.cloud.callFunction({
          name: 'matchUsers',
          data: {
            hobby: that.data.hobby
          },
          success: res => {
            if (res.result.code === 0) {
              const matchedUsers = res.result.data;
              const randomIndex = Math.floor(Math.random() * matchedUsers.length); // 随机选择一个索引
              const randomUser = matchedUsers[randomIndex]; // 获取随机选择的用户
              that.setData({
                matchedUsers: [randomUser] // 将随机选择的用户放入数组中
              });
            } else {
              wx.showToast({
                title: '匹配失败',
                icon: 'none'
              });
            }
          },
          fail: err => {
            console.error(err);
            wx.showToast({
              title: '匹配失败',
              icon: 'none'
            });
          }
        });
      },
      // 点击用户信息项跳转到聊天页面
  goToChatPage: function (event) {
    const userId = event.currentTarget.dataset.userid; // 获取点击的用户ID
    wx.navigateTo({
      url: '/pages/chatroom/chatroom?userId=' + userId, // 跳转到聊天页面，并携带用户ID参数
    });
  },
    goto1(){
        wx.navigateTo({
          url: '/pages/a11/a11',
        })
    },
    goto2(){
        wx.navigateTo({
          url: '/pages/a12/a12',
        })
    },
      //测试按钮
      startAnimation(){
          let that = this;
  
          //x轴位移
          let h1 = "35%";
          let h2 = "65%";
          //用于延迟的函数
          const sleep = (time)=>{
              return new Promise((res)=> setTimeout(res,time))
          }
          if(this.data.bar_state == 1){	
              h1 = "40%";
              h2 = "40%";	
              setTimeout(() => {
                  that.setData({
                      bar_state:0,
                      hasResult:0
                  })
                  setTimeout(() => {
                      that.setData({
                          hasResult:1
                      })
                  }, 4000);
              }, 400);
          }else{
              
              h1 = "25%";
              h2 = "55%";
  
  
              that.setData({
                  bar_state:1
              })
              setTimeout(() => {
                  that.startAnimation()
              }, 600);
          }
          this.animation1.height(h1).step();
          this.animation2.top(h2).step();
          this.setData({
              animation1:that.animation1.export(),
              animation2:that.animation2.export()
          })
      },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
          let that = this;
          that.initAnimation();
          //重力加速度
  
          wx.startAccelerometer({
              success(e){
                  wx.onAccelerometerChange((res) => {
                      console.log(res);
                      // console.log(res.x);
                      // console.log(res.y);
                      // console.log(res.z);
                      if(res.x > .7 && res.y > .7){
                          wx.showToast({
                              title: '摇一摇成功',
                              icon:"success",
                              duration:2000
                          })
                          that.startAnimation();
                      
                      }
                  })
              }
          })
  
      
          wx.getSystemInfo({
              success(e){
                  that.setData({
                      winWidth:e.windowWidth,
                      winHeight:e.windowHeight
                  })
              }
          })
    },
      initAnimation(){
          this.animation1 = wx.createAnimation({
              duration:400,
              timingFunction:"ease",
              transformOrigin:"left top 0",
              success(e){
                  console.log(e);
              }
          })
          this.animation2 = wx.createAnimation({
              duration:400,
              timingFunction:"ease",
              transformOrigin:"left top 0",
              success(e){
                  console.log(e);
              }
          })
      }
  })